import wxvalidate from'wechat-validate'
Page({
  behaviors: [wxvalidate],
  data: {
    currentDate:new Date().getTime(),
    minDate: new Date().getTime(),
    appointment:"",
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    houseId:"",
    houseInfo:"",
    mobile:"",
    description:'',
    repairItemId:''
  },
  rules: {
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
  },
  onLoad({id}){
    this.getHouseLIst()
    this.getRepairItem()
    console.log(id);
    if (id) this.getRepairDetail(id)
  },
  async getRepairDetail(id) {
    // 调用接口
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 检测是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ ...repairDetail })
  },
  selectDateInfo(ev){
      console.log(ev);
      this.setData({
        appointment: wx.utils.dataformate(ev.detail),
        dateLayerVisible: false
      });
    },
async getHouseLIst(){
const {data:houseList,code}=await wx.http.get('/house')
if(code!==10000) return wx.utils.toast()
// console.log(data);
this.setData({
  houseList
 })
 },
 selectHouseInfo(ev){
console.log(ev.detail);
this.setData({houseId:ev.detail.id,housename:ev.detail.name})
 },
 async getRepairItem(){
  const {data:repairItem,code}=await wx.http.get('/repairItem')
  if(code!==10000) return wx.utils.toast()
  // console.log(data);
  this.setData({
    repairItem
   })
   },
seletRepairItem(ev){
    console.log(ev.detail);
    this.setData({repairItemId: ev.detail.id,
      repairItemName: ev.detail.name,})
     },
uploadPicture(ev){
      const { file } = ev.detail;
      wx.uploadFile({
        url:wx.http.baseURL+'/upload', 
        filePath: file.url,
        name: 'file',
        header: {
          Authorization: 'Bearer ' + getApp().token,
        },
        success:(result)=>{
          const data = JSON.parse(result.data)
          if (data.code !== 10000) return wx.utils.toast('文件上传失败!')
          const { attachment } = this.data
          attachment.push(data.data);
          this.setData({attachment});
        },
      });
     },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
 async submitForm() {
    if(!this.validate()) return
    const {id,houseId,repairItemId,mobile,appointment,description,attachment}=this.data
    const {code}=await wx.http.post('/repair',{id,houseId,repairItemId,mobile,appointment,description,attachment})
    if(code!==10000)return wx.utils.toast('在线报修失败')
    
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
  async cancelRepair(ev) {
    // 调用接口
    const { code } = await wx.http.put('/cancel/repaire/' + ev.mark.id)
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 跳转到报修列表页面
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
